<script setup lang="ts">

import router from '@/router';
import ProblemTag from '@/components/Problem/ProblemTag.vue';

defineProps({
  list: Array,
  columns: Array,
});


const handleSubmissionClick = (submissionId: string) => {
  if (!router.currentRoute.value.path.includes('contest'))
    window.open(`/submission/${submissionId}`, '__blank');
};

const handleUserClick = (personName: string) => {
  const url = `${window.location.origin}/profile?username=${encodeURIComponent(personName)}`;
  window.open(url, '_blank');
};

const handleProblemClick = (problemId: number) => {
  const url = `${window.location.origin}/problem/${problemId}`;
  window.open(url, '_blank');
};
</script>


<template>
  <div>
    <a-table :columns="columns" :data-source="list" class="submit-list" :pagination="false">
      <template #bodyCell="{ text, record, column }">
        <template v-if="column.key === 'personId'">
          <a class="stretched-link text-decoration-none" @click="handleUserClick(record.username)">
            {{ record.username || '不知名的用户' }}
          </a>
        </template>
        <template v-else-if="column.key === 'remoteVerdict'">
          <a v-if="record.remoteVerdict==='Accepted'" class="result-right  stretched-link text-decoration-none"
             @click="handleSubmissionClick(record._id)">
            {{ record.remoteVerdict }}
          </a>
          <a v-else class="result-error stretched-link text-decoration-none" @click="handleSubmissionClick(record._id)">
            {{ record.remoteVerdict || 'Pending' }}
          </a>
        </template>
        <template v-else-if="column.key === 'problemId'">
          <a class="stretched-link text-decoration-none" @click="handleProblemClick(record.problemId)">
            {{ text }}
          </a>
        </template>
        <template v-if="column.key === 'tags'">
          <template v-for="(tag, index) in record.tags" :key="index">
            <problem-tag :tag=tag></problem-tag>
          </template>
          <template v-if="record.tags.length === 0">
            <problem-tag :tag="{
              name:'识别中..',
              color:'#868886'
            }"></problem-tag>
          </template>
        </template>
        <template v-else-if="column.key === 'time'">
          <span>
            {{ record.result.time !== undefined ? record.result.time : 'N/A' }}
          </span>
        </template>
        <template v-else-if="column.key === 'memory'">
          <span>
            {{ record.result.memory || 'N/A' }}
          </span>
        </template>
        <template v-else-if="column.key === 'lang'">
          <span>
            {{ record.lang || 'N/A' }}
          </span>
        </template>
        <template v-else-if="column.key === 'createTime'">
          <span>
            {{ new Date(record.createTime).toLocaleString() }}
          </span>
        </template>
      </template>
    </a-table>
  </div>
</template>

<style scoped>
.result-error {
  color: red;
}

.result-right {
  color: green;
}
</style>

<style>
.submit-list .ant-table-cell {
  padding: 8px;
}
</style>


